import pandas as pd
from pyecharts.charts import WordCloud
from pyecharts.globals import ThemeType
from snownlp import SnowNLP
from pyecharts import options as opts

from demo.dataSource.Data import remark_data

remark_data['词性标注'] = remark_data['评论'].agg(lambda x:list(SnowNLP(x).tags))

list_tags = []
for i in remark_data['词性标注']:
    for j in i:
        list_tags.append(j)

data_tags = pd.DataFrame(list_tags,columns=['词语','词性'])
data_tags['词性'].value_counts()
result = data_tags[data_tags['词性']=='a'].groupby(by='词语')['词语'].count().sort_values(ascending=False)#[:20]
data_wordcloud_tags = [(i,str(j)) for i,j in zip(result.index,result.values)]

word = (
    WordCloud(init_opts=opts.InitOpts(theme=ThemeType.LIGHT, width="400px",
                                      height="400px", ))
        .add(series_name="",
             data_pair=data_wordcloud_tags,
             word_size_range=[20, 70],
             #          mask_image="./口罩.jpg",

             textstyle_opts=opts.TextStyleOpts(font_family="cursive"),  # 文字的字体系列
             shape='cardioid',  # 词云图轮廓:心形
             )
        .set_global_opts(
        title_opts=opts.TitleOpts(
            title="近期关于疫情的评论热词",
            pos_top='16%',
            #             pos_left='10%'
            title_textstyle_opts=opts.TextStyleOpts(color='#CCFFFF', )

        ),
        tooltip_opts=opts.TooltipOpts(is_show=True),

    )
)
word.render("../outHTML/word.html")